package org.come.Jpanel;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.tool.tcpimg.*;
import org.come.Frame.FlyJram;
import org.come.Frame.ZhuFrame;
import org.come.bean.PathPoint;
import org.come.bean.Skill;
import org.come.control.TestMain;
import org.come.mouslisten.GoodsMouslisten;
import org.come.test.Main;
import org.come.until.*;

import com.tool.image.ImageMixDeal;
import com.tool.tcp.Sprite;
import com.updateNew.MyIsif;

import come.tool.Fighting.FightingMixDeal;
import come.tool.handle.HandleState;

public class GameJpanel extends JPanel {


    public static Skill skill;
    private static GameJpanel gameJpanel;
    public static Image imagefly = flyunit.image;

    public static GameJpanel getGameJpanel() {
        if (gameJpanel != null) {
            return gameJpanel;
        } else {
            gameJpanel = new GameJpanel();
            return gameJpanel;
        }
    }

    /**
     * 系统提示对话
     */
    private List<TipBox> SystemPrompt = new ArrayList<>();
    private List<NoticeBox> SystemNotice = new ArrayList<>();
    private JLabel mouse;
    private SystemBox systemBox;
    private SystemBoos systemBoos;

    ImageIcon icon;
    ImageIcon image;


    public GameJpanel() {

        // TODO Auto-generated constructor stub
        this.setBackground(UIUtils.Color_BACK);
        this.setPreferredSize(new Dimension(ScrenceUntil.Screen_x + ScrenceUntil.ChatFram_X, ScrenceUntil.Screen_y));
        this.setLayout(null);
        try {
            mouse = new JLabel() {
                @Override
                protected void paintComponent(Graphics g) {
                    // TODO Auto-generated method stub
                    super.paintComponent(g);
                    // 铃铛显示位置-公告显示

                    if (isNotice) {
                        int chatY = 100;
                        g.translate(0, chatY);
                        for (int i = SystemNotice.size() - 1; i >= 0; i--) {
                            NoticeBox chatPanel = SystemNotice.get(i);
                            if (chatPanel.IsTime()) {
                                g.translate(0, -chatPanel.getHeight());
                                chatY -= chatPanel.getHeight();
                                chatPanel.paint(g);
                            } else {
                                SystemNotice.remove(i);
                            }
                        }
                        g.translate(0, -chatY);
                        if (SystemNotice.size() == 0) {
                            isNotice = false;
                        }
                    }
//                    if (ScrenceUntil.Screen_x == 800 && ScrenceUntil.Screen_y == 600) {
//                        if (isNotice) {
//                            int chatY = ScrenceUntil.Screen_y - 468;
//
//                            g.translate(0, chatY);
//                            for (int i = SystemNotice.size() - 1; i >= 0; i--) {
//                                NoticeBox chatPanel = SystemNotice.get(i);
//                                if (chatPanel.IsTime()) {
//                                    g.translate(0, -chatPanel.getHeight());
//                                    chatY -= chatPanel.getHeight();
//                                    chatPanel.paint(g);
//                                } else {
//                                    SystemNotice.remove(i);
//                                }
//                            }
//                            g.translate(0, -chatY);
//                            if (SystemNotice.size() == 0) {
//                                isNotice = false;
//                            }
//                        }
//                    } else if (ScrenceUntil.Screen_x == 1024 && ScrenceUntil.Screen_y == 768) {
//                        if (isNotice) {
//                            int chatY = ScrenceUntil.Screen_y - 592;
//
//                            g.translate(0, chatY);
//                            for (int i = SystemNotice.size() - 1; i >= 0; i--) {
//                                NoticeBox chatPanel = SystemNotice.get(i);
//                                if (chatPanel.IsTime()) {
//                                    g.translate(0, -chatPanel.getHeight());
//                                    chatY -= chatPanel.getHeight();
//                                    chatPanel.paint(g);
//                                } else {
//                                    SystemNotice.remove(i);
//                                }
//                            }
//                            g.translate(0, -chatY);
//                            if (SystemNotice.size() == 0) {
//                                isNotice = false;
//                            }
//                        }
//                    } else if (ScrenceUntil.Screen_x == 1366 && ScrenceUntil.Screen_y == 768) {
//                        if (isNotice) {
//                            int chatY = ScrenceUntil.Screen_y - 592;
//
//                            g.translate(0, chatY);
//                            for (int i = SystemNotice.size() - 1; i >= 0; i--) {
//                                NoticeBox chatPanel = SystemNotice.get(i);
//                                if (chatPanel.IsTime()) {
//                                    g.translate(0, -chatPanel.getHeight());
//                                    chatY -= chatPanel.getHeight();
//                                    chatPanel.paint(g);
//                                } else {
//                                    SystemNotice.remove(i);
//                                }
//                            }
//                            g.translate(0, -chatY);
//                            if (SystemNotice.size() == 0) {
//                                isNotice = false;
//                            }
//                        }
//                    } else if (ScrenceUntil.Screen_x == 1920 && ScrenceUntil.Screen_y == 1080) {
//                        if (isNotice) {
//                            int chatY = ScrenceUntil.Screen_y - 905;
//
//                            g.translate(0, chatY);
//                            for (int i = SystemNotice.size() - 1; i >= 0; i--) {
//                                NoticeBox chatPanel = SystemNotice.get(i);
//                                if (chatPanel.IsTime()) {
//                                    g.translate(0, -chatPanel.getHeight());
//                                    chatY -= chatPanel.getHeight();
//                                    chatPanel.paint(g);
//                                } else {
//                                    SystemNotice.remove(i);
//                                }
//                            }
//                            g.translate(0, -chatY);
//                            if (SystemNotice.size() == 0) {
//                                isNotice = false;
//                            }
//                        }
//                    }
                    if (isBox) {
                        int chatY = ScrenceUntil.Screen_y / 2;
                        g.translate(0, chatY);
                        for (int i = SystemPrompt.size() - 1; i >= 0; i--) {
							TipBox chatPanel = SystemPrompt.get(i);
                            if (chatPanel.IsTime()) {
                                g.translate(0, -chatPanel.getHeight());
                                chatY -= chatPanel.getHeight();
                                chatPanel.paint(g);
                            } else {
                                SystemPrompt.remove(i);
                            }
                        }
                        g.translate(0, -chatY);
                        if (SystemPrompt.size() == 0) {
                            isBox = false;
                        }
                    }
                    if (systemBox != null) {
                        systemBox.draw(g);
                    }
					PathPoint point = mousepath();
                    try {
                        if (GoodsMouslisten.replacepath != -1) {
                            icon = GoodsListFromServerUntil.imgpath(GoodsListFromServerUntil.getGoodslist()[GoodsMouslisten.replace].getSkin());
                            g.drawImage(icon.getImage(), point.getX() - 25, point.getY() - 25, 49, 49, this);
                        }

						if(skill != null){
							ImageIcon image = CutButtonImage.getImage("inkImg/old/skill/fighting-skill/" + skill.getSkillid() + ".png", 40, 40);
							g.drawImage(image.getImage(), point.getX()-17,point.getY()-17, 35,35, this);
						}

                    } catch (Exception e) {
                        // TODO: handle exception
                        GoodsMouslisten.replacepath = -1;
                    }
					Sprite mouse = MessagrFlagUntil.getMouse();
					if (mouse != null) {
						mouse.updateToTime(ImageMixDeal.userimg.getTime(), 0);
						mouse.draw(g, point.getX(), point.getY());
                    }
                }
            };
            mouse.setBounds(0, 0, ScrenceUntil.Screen_x + ScrenceUntil.ChatFram_X, ScrenceUntil.Screen_y);
            this.add(mouse);
            this.add(ZhuFrame.getzhuframe());
            this.setBackground(Color.BLACK);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 获取鼠标的位置
     */
    private PathPoint pathPoint = new PathPoint(0, 0);
//	public PathPoint mousepath() {
//		Point p = MouseInfo.getPointerInfo().getLocation();
//		PathPoint p1 = Main.pathPoint;
//		pathPoint.setX((int) (p.getX() - p1.getX()));
//		pathPoint.setY((int) (p.getY() - p1.getY()));
//		return pathPoint;
//	}

    public PathPoint mousepath() {

        //多开变单开
        String a = MyIsif.ifs;
//		a = "DK";
        if (a != null) {
            if (!"D".equals(a)) {
                Point p = MouseInfo.getPointerInfo().getLocation();
                PathPoint p1 = Main.pathPoint;
                pathPoint.setX((int) (p.getX() - p1.getX()));
                pathPoint.setY((int) (p.getY() - p1.getY()));
                return pathPoint;
            } else {
                Point p = MouseInfo.getPointerInfo().getLocation();
                Point p1 = TestMain.gameJframe.getLocation();
                pathPoint.setX((int) (p.getX() - p1.getX()) - 4);
                pathPoint.setY((int) (p.getY() - p1.getY()) - 26);
                return pathPoint;
            }
        } else {
            Point p = MouseInfo.getPointerInfo().getLocation();
            PathPoint p1 = Main.pathPoint;
            pathPoint.setX((int) (p.getX() - p1.getX()));
            pathPoint.setY((int) (p.getY() - p1.getY()));
            return pathPoint;
        }

    }

    // 流畅度质变
    static final int DEFAULT_FPS = 60;
    static final int DEFAULT_CHA = 1000000;
    // 计算出指定FPS数值中，每帧需要多少时间
    static final long fpsTime = (long) ((Double.valueOf(1000) / Double.valueOf(DEFAULT_FPS)) * 1000000);
    // 绘制图像前的时间戳
    long now = System.nanoTime();
    long now2 = 0;
    // 每次绘制图像耗时（毫秒）
    long total = 0;

    public static boolean isAlpha = false;
    public static float alpha = 1.0f;




    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        FlyJram.getFrames();
        if (alpha < 1) {
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        }
        // 判断是否处于战斗状态
        if (FightingMixDeal.State == HandleState.USUAL) {
            ImageMixDeal.Drawing(g2d, total);
        } else {
//			ImageMixDeal.move2(total);
            ImageMixDeal.userimg.setTime(total);
            FightingMixDeal.Drawing(g2d, total);
        }
        if (FrameMessageChangeJpanel.chatbox.isDisplay()) {
            Graphics g22 = g.create(0, ScrenceUntil.Screen_y - 45- FrameMessageChangeJpanel.chatbox.getH(),FrameMessageChangeJpanel.chatbox.getW(),FrameMessageChangeJpanel.chatbox.getH());
            FrameMessageChangeJpanel.chatbox.paint(g22);
            g22.dispose();
        }
        try {
            total = fpsTime - System.nanoTime() + now;
            if (alpha < 1) {
                alpha += 0.03f;
            }
            if (total > 0) {
                Thread.sleep(total / DEFAULT_CHA);
            } else {
                Thread.sleep(1);
            }
            now2 = System.nanoTime();
            total = (now2 - now) / DEFAULT_CHA;
            now = now2;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 移除持续重绘调用，改用条件重绘
        // repaint();
    }

    public JLabel getMouse() {
        return mouse;
    }

    public void setMouse(JLabel mouse) {
        this.mouse = mouse;
    }

    /**
     * 添加对话框
     */
    boolean isBox = false;

    public void addPrompt(String text) {
        if (text != null) {
            SystemPrompt.add(new TipBox(text));
            isBox = true;
        }
    }

    /**
     * 添加对话框
     */
    boolean isNotice = false;

    public void addNotice(String text) {
        if (text != null) {
            //SystemNotice.clear();
            SystemNotice.add(new NoticeBox(text));
            isNotice = true;
        }
    }

    /**
     * 添加滚动字体 8广告滚动   9系统滚动
     */
    public void addBox(String text, int type, String skin) {
        if (systemBox == null) {
            systemBox = new SystemBox(text, skin != null ? skin : "HF1");
        } else {
            systemBox.addText(text);
        }
    }

    /**
     * 10系统boos消息
     */
    public void addBoos(String text, int type, String skin) {
        if (systemBoos == null) {
            systemBoos = new SystemBoos(text, skin != null ? skin : "三界异象");
        } else {
            systemBoos.addText(text);
        }
    }
}

